package at.ac.tuwien.infosys.aic11.services;

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor;
import org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor;
import org.apache.ws.security.handler.WSHandlerConstants;
import org.eclipse.jetty.util.log.Log;

import at.ac.tuwien.infosys.aic11.dto.CreditRequest;
import at.ac.tuwien.infosys.aic11.dto.Offer;
import at.ac.tuwien.infosys.aic11.fault.UnknownCustomerFault;
import at.ac.tuwien.infosys.aic11.fault.UnknownDurationFault;
import at.ac.tuwien.infosys.aic11.fault.UnknownMoneyFault;
import at.ac.tuwien.infosys.aic11.fault.UnknownRatingFault;
import at.ac.tuwien.infosys.aic11.fault.UnknownRequestFault;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: gh
 * Date: 10.11.11
 * Time: 13:17
 * To change this template use File | Settings | File Templates.
 */
@ApplicationScoped
@Named
public class ContractManagementServiceClient {

	private ContractManagement client;
	
	private static Logger log = Logger.getLogger("ContractManagementImpl");


	public ContractManagementServiceClient(){
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.getInInterceptors().add(new LoggingInInterceptor());
		factory.getOutInterceptors().add(new LoggingOutInterceptor());

		Map<String, Object> inProps = new HashMap<String, Object>();
		inProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.TIMESTAMP
				+ " " + WSHandlerConstants.SIGNATURE + " "
				+ WSHandlerConstants.ENCRYPT);
		inProps.put(WSHandlerConstants.SIG_PROP_FILE,
				"META-INF/clerk_cert.properties");
		inProps.put(WSHandlerConstants.DEC_PROP_FILE,
				"META-INF/clerk_cert.properties");
		inProps.put(WSHandlerConstants.PW_CALLBACK_CLASS,
				at.ac.tuwien.infosys.aic11.testclient.PasswordHandler.class.getName());

		WSS4JInInterceptor wssIn = new WSS4JInInterceptor(inProps);
		factory.getInInterceptors().add(wssIn);

		Map<String, Object> outProps = new HashMap<String, Object>();
		outProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.TIMESTAMP
				+ " " + WSHandlerConstants.SIGNATURE + " "
				+ WSHandlerConstants.ENCRYPT);
		outProps.put(WSHandlerConstants.USER, "clerk");
		outProps.put(WSHandlerConstants.PW_CALLBACK_CLASS,
				at.ac.tuwien.infosys.aic11.testclient.PasswordHandler.class.getName());
		outProps.put(WSHandlerConstants.ENCRYPTION_USER, "crmserver");
//		outProps.put(WSHandlerConstants.ENC_SYM_ALGO, "http://www.w3.org/2001/04/xmlenc#tripledes-cbc");
//		outProps.put(WSHandlerConstants.ENC_KEY_TRANSPORT, "http://www.w3.org/2001/04/xmlenc#rsa-1_5");
		outProps.put(WSHandlerConstants.SIG_PROP_FILE,
				"META-INF/clerk_cert.properties");
		outProps.put(WSHandlerConstants.ENC_PROP_FILE,
				"META-INF/clerk_cert.properties");

		WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(outProps);
		factory.getOutInterceptors().add(wssOut);

		Log.info("wss4j setup done");

		factory.setServiceClass(ContractManagement.class);
		factory.setAddress("http://localhost:9001/ContractManagementService");
		client = (ContractManagement) factory.create();
	}

	public Offer getOffer(CreditRequest creditRequest) throws UnknownRequestFault, UnknownCustomerFault, UnknownMoneyFault, UnknownRatingFault, UnknownDurationFault {
		return client.getOffer(creditRequest);
	}

	public void deleteOffer(CreditRequest creditRequest)  throws UnknownRequestFault, UnknownCustomerFault, UnknownMoneyFault, UnknownRatingFault, UnknownDurationFault {
		client.deleteOffer(creditRequest);
	}
}
